/************************************************************************************/
/*  Program name CAPAPIMP_SELECT_INV.sql                                            */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_SELECT_INV';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_SELECT_INV;
	
	lv_SOB_Name		NUMBER;
	lv_COA_ID		NUMBER;
	lv_Vend_ID		NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			lv_SOB_Name := 	CAPBRIMP_COMMON.GetSOBShortName(C1.SET_OF_BOOKS_ID);
			lv_COA_ID	:=	CAPBRIMP_COMMON.GetCOAID(lv_SOB_Name);
			lv_Vend_ID	:=	CAPBRIMP_COMMON.GetVendorID_Name(C1.VENDOR_NAME);
		
			INSERT INTO AP_SELECTED_INVOICES_ALL
			VALUES
			(
			C1.CHECKRUN_NAME,
			CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(C1.INVOICE_ID),	--INVOICE_ID,
			C1.PAYMENT_NUM,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),			--SET_OF_BOOKS_ID,
			C1.LAST_UPDATE_LOGIN,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			lv_Vend_ID,												--VENDOR_ID,
			CAPBRIMP_COMMON.GetVendorSiteID(lv_Vend_ID, C1.ORG_ID, C1.VENDOR_SITE_CODE),	--VENDOR_SITE_ID,
			C1.VENDOR_NUM,
			C1.VENDOR_NAME,
			C1.VENDOR_SITE_CODE,
			C1.CUSTOMER_NUM,
			C1.ADDRESS_LINE1,
			C1.ADDRESS_LINE2,
			C1.ADDRESS_LINE3,
			C1.CITY,
			C1.STATE,
			C1.ZIP,
			C1.PROVINCE,
			C1.COUNTRY,
			C1.WITHHOLDING_STATUS_LOOKUP_CODE,
			C1.ATTENTION_AR_FLAG,
			C1.INVOICE_NUM,
			C1.INVOICE_DATE,
			C1.VOUCHER_NUM,
			C1.INVOICE_AMOUNT,
			C1.AMOUNT_PAID,
			C1.DISCOUNT_AMOUNT_TAKEN,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.AP_CCID_D),			--AP_CCID,
			C1.DUE_DATE,
			C1.DISCOUNT_DATE,
			C1.INVOICE_DESCRIPTION,
			C1.PAYMENT_PRIORITY,
			C1.AMOUNT_REMAINING,
			C1.DISCOUNT_AMOUNT_REMAINING,
			C1.PAYMENT_AMOUNT,
			C1.DISCOUNT_AMOUNT,
			C1.SEQUENCE_NUM,
			C1.OK_TO_PAY_FLAG,
			C1.DONT_PAY_REASON_CODE,
			C1.ALWAYS_TAKE_DISCOUNT_FLAG,
			C1.AMOUNT_MODIFIED_FLAG,
			C1.CHECK_NUMBER,
			C1.INVOICE_EXCHANGE_RATE,
			C1.PAYMENT_CROSS_RATE,
			C1.BANK_ACCOUNT_TYPE,
			C1.ORIGINAL_INVOICE_ID,										--InvoiceID
			C1.ORIGINAL_PAYMENT_NUM,
			C1.ATTRIBUTE_CATEGORY,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.BANK_ACCOUNT_NUM,
			C1.BANK_NUM,
			C1.FUTURE_PAY_DUE_DATE,
			C1.EXCLUSIVE_PAYMENT_FLAG,
			C1.PROPOSED_PAYMENT_AMOUNT,
			CAPBRIMP_MAPPING.GetAPSelInvCheckIDMapping(C1.PAY_SELECTED_CHECK_ID),	--PAY_SELECTED_CHECK_ID
			CAPBRIMP_MAPPING.GetAPSelInvCheckIDMapping(C1.PRINT_SELECTED_CHECK_ID),	--PRINT_SELECTED_CHECK_ID
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),					--ORG_ID,
			C1.WITHHOLDING_AMOUNT,
			C1.INVOICE_PAYMENT_ID,
			C1.DONT_PAY_DESCRIPTION,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.TRANSFER_PRIORITY,
			CAPBRIMP_MAPPING.GetBankACMapping(C1.EXTERNAL_BANK_ACCOUNT_ID),
			C1.ADDRESS_LINE4,
			C1.IBAN_NUMBER
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
